import requests
import json


# 获取token
def get_access_token():
    url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
    values = {
        'corpid': 'ww7b27fdad31de679c',
        'corpsecret': '4KxvbjYu6vdu9C5QbfmEFKrHQuH0gwIkpc7dYjiCJtw'
    }
    req = requests.post(url, json=values)
    data = json.loads(req.text)
    return data["access_token"]

# 获取模板内容
def get_template_info(token):
    url = "https://qyapi.weixin.qq.com/cgi-bin/oa/gettemplatedetail?access_token="+token
    values = {
        "template_id": "ZLycYwqGpXjTxgfHcdrtgcnqwJusD6JaETYgV7" #测试模板id
    }
    req = requests.post(url, json=values)
    data = json.loads(req.text)
    return data


# 获取审批详细内容
def get_approval_info(token):
    url = 'https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovaldetail?access_token='+token
    values = {
        "sp_no": 202007240006
    }
    req = requests.post(url, json=values)
    data = json.loads(req.text)
    return data


# 获取指定时间内的审批单号
def get_approval_number(token):
    url = 'https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovalinfo/?access_token='+token
    values = {
        "starttime": "1595520000",
        "endtime": "1595779200",
        "cursor": 0,
        "size": 100,
        "filters": [
            {"key": "template_id", "value": "Bs7xbiw4u2CihouC6fsG8iLZKgELeNFxAK8TrN1J5"},
            {"key": "creator", "value": "JiXiaoYun"}
        ]
    }
    req = requests.post(url, json=values)
    data = json.loads(req.text)
    return data


access_token = get_access_token()
print("token值："+access_token)

template_data = get_template_info(access_token)
print("模板详情："+template_data)

approval_info = get_approval_info(access_token)
print("审批详情："+approval_info)

approval_number = get_approval_number(access_token)
print(approval_number)
